<?php
class IntegralrankingModel extends Model {
	//列表
	public function _list($view,$map,$sort='sort',$pagesize=20){
		//计算总页数
		$count=$this->where($map)->count();
		//实例化分页类
		import ( 'ORG.Util.Page' );
		$p=new Page($count,$pagesize);
		$list = $this->where($map)->order($sort)->limit($p->firstRow.',
'.$p->listRows)->select();
        foreach($list as $k => $v){
			$list[$k]['progress'] = M('Schedule') -> where(array('mission_id' => $v['id'])) -> order('datatime desc') -> select();
		}
        $pages=$p->show();
		$view->assign('list',$list);
		$view->assign('pages',$pages);
	}
	//从积分日志表中转换数据
	public function getDataFromLog($data){
		$data['integraltypeid'] = $data['type_id'];
		$memberpositionid = M('Member') -> where(array('id' => $data['member_id'])) -> getField('memberpositionid');
		$data['memberpositionid'] = $memberpositionid;
		$data['total'] = $data['totalnum'];
		$data['num'] = $data['changenum'];
		$data['dateline'] = $data['time'];
		return $this->add($data);
	}

	public function updateData($data){
		if(isset($data['logid'])){
			unset($data['id']);
			$data['num']=$data['changenum'];
			$data['integrallogid']=$data['logid'];
			$data['dateline']=$data['time'];
			if (!empty($data)) {
				if (!$this->create($data)) {
					$this->error($this->getError());
				}
			}else{
				if (!$this->create()) {
					$this->error($this->getError());
				}
			}
			$result = $this->where('integrallogid ='.$data['integrallogid'])->save($data);
			if (false !== $result) {
			} else {
				$this->error($this->getError());
			}
			return $result;
		}
	}
	//总积分
//	public function totalIntegral($memberid){
//		$r=$this->where('member_id ='.$memberid)->order('id desc')->getfield('total');
//		return $r;
//	}
}